package com.studio.bill.swiperecyclerdemo.util.libimage;

import android.graphics.Bitmap;

/**
 * @author: tangjie
 * @date: 2017/3/22 11:09
 * @Description: 图片配置
 */

public class ImageConfig {
    public static final int SCALE_FIT_IMAGE = 1;
    private int imageResOnLoading;
    private int imageResForEmptyUri;
    private int imageResOnFail;
    private boolean cacheInMemory;
    private boolean cacheOnDisk;
    private int roundCornerRadius;
    private int scaleType;
    private Bitmap.Config config;
    private int fadeInTime;

    public int getFadeInTime() {
        return fadeInTime;
    }

    public void setFadeInTime(int fadeInTime) {
        this.fadeInTime = fadeInTime;
    }

    public Bitmap.Config getConfig() {
        return config;
    }

    public void setConfig(Bitmap.Config config) {
        this.config = config;
    }

    public int getImageResOnLoading() {
        return imageResOnLoading;
    }

    public int getRoundCornerRadius() {
        return roundCornerRadius;
    }

    public void setRoundCornerRadius(int roundCornerRadius) {
        this.roundCornerRadius = roundCornerRadius;
    }

    public void setImageResOnLoading(int imageResOnLoading) {
        this.imageResOnLoading = imageResOnLoading;
    }

    public int getImageResForEmptyUri() {
        return imageResForEmptyUri;
    }

    public void setImageResForEmptyUri(int imageResForEmptyUri) {
        this.imageResForEmptyUri = imageResForEmptyUri;
    }

    public int getImageResOnFail() {
        return imageResOnFail;
    }

    public void setImageResOnFail(int imageResOnFail) {
        this.imageResOnFail = imageResOnFail;
    }

    public boolean isCacheInMemory() {
        return cacheInMemory;
    }

    public void setCacheInMemory(boolean cacheInMemory) {
        this.cacheInMemory = cacheInMemory;
    }

    public boolean isCacheOnDisk() {
        return cacheOnDisk;
    }

    public void setCacheOnDisk(boolean cacheOnDisk) {
        this.cacheOnDisk = cacheOnDisk;
    }


    public static ImageConfig getDefaultImgOptions() {
        return new Builder().cacheInMemory(true)
                .cacheOnDisc(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .build();
    }

    public static ImageConfig getImgOptions(int imageResOnLoading, int imageResOnFail) {
        return new Builder().cacheInMemory(true)
                .cacheOnDisc(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .showImageOnLoading(imageResOnLoading)
                .showImageOnOnFail(imageResOnFail)
                .showImageOnEmpty(imageResOnFail)
                .build();
    }

    public static ImageConfig getFitImgOptions(int imageResOnLoading) {
        return new Builder().cacheInMemory(true)
                .cacheOnDisc(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .showImageOnLoading(imageResOnLoading)
                .showImageOnOnFail(imageResOnLoading)
                .showImageOnEmpty(imageResOnLoading)
                .onImageScalType(ImageConfig.SCALE_FIT_IMAGE)
                .build();
    }

    public static ImageConfig getRoundCornerImgOptions(int radius) {
        return new Builder().cacheInMemory(true)
                .cacheOnDisc(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .showRoundCorner(radius)
                .build();
    }

    public static ImageConfig getRoundCornerImgOptions(int imageRes, int radius) {
        return new Builder().cacheInMemory(true)
                .cacheOnDisc(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .showImageOnLoading(imageRes)
                .showImageOnOnFail(imageRes)
                .showImageOnEmpty(imageRes)
                .showRoundCorner(radius)
                .build();
    }

    public static ImageConfig getFadeInImgOptions(int imageRes, int time) {
        return new Builder().cacheInMemory(true)
                .cacheOnDisc(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .showImageOnLoading(imageRes)
                .showImageOnOnFail(imageRes)
                .showImageOnEmpty(imageRes)
                .fadeInTime(time)
                .build();
    }

    public static ImageConfig getImgOptions(int imageRes) {
        return new Builder().cacheInMemory(true)
                .cacheOnDisc(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .showImageOnLoading(imageRes)
                .showImageOnOnFail(imageRes)
                .showImageOnEmpty(imageRes)
                .build();
    }

    public static ImageConfig getHomepageImgOptions(int imageRes) {
        return new Builder().cacheInMemory(true)
                .cacheOnDisc(true)
                .bitmapConfig(Bitmap.Config.ARGB_8888)
                .showImageOnLoading(imageRes)
                .showImageOnOnFail(imageRes)
                .showImageOnEmpty(imageRes)
                .build();
    }

    public static ImageConfig getScalImgOptions(int imageRes, int scaleType) {
        return new Builder().cacheInMemory(true)
                .cacheOnDisc(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .showImageOnLoading(imageRes)
                .showImageOnOnFail(imageRes)
                .showImageOnEmpty(imageRes)
                .onImageScalType(scaleType)
                .build();
    }

    private ImageConfig(Builder builder) {
        this.imageResOnLoading = builder.imageResOnLoading;
        this.imageResForEmptyUri = builder.imageResForEmptyUri;
        this.imageResOnFail = builder.imageResOnFail;
        this.cacheInMemory = builder.cacheInMemory;
        this.cacheOnDisk = builder.cacheOnDisk;
        this.roundCornerRadius = builder.roundCornerRadius;
        this.fadeInTime = builder.fadeInTime;
        this.scaleType = builder.scalType;
    }

    public int getScaleType() {
        return scaleType;
    }

    public void setScaleType(int scaleType) {
        this.scaleType = scaleType;
    }

    public static class Builder {
        private int imageResOnLoading = 0;
        private int imageResForEmptyUri = 0;
        private int imageResOnFail = 0;
        private boolean cacheInMemory = false;
        private boolean cacheOnDisk = false;
        private int roundCornerRadius;
        private Bitmap.Config config = Bitmap.Config.RGB_565;
        private int fadeInTime;
        private int scalType;

        public Builder cacheInMemory(boolean cacheInMemory) {
            this.cacheInMemory = cacheInMemory;
            return this;
        }

        public Builder cacheOnDisc(boolean flag) {
            this.cacheOnDisk = true;
            return this;
        }

        public Builder bitmapConfig(Bitmap.Config bitmapConfig) {
            if (bitmapConfig == null)
                throw new IllegalArgumentException("bitmapConfig can't be null");
            this.config = bitmapConfig;
            return this;
        }

        public Builder showImageOnLoading(int imageRes) {
            imageResOnLoading = imageRes;
            return this;
        }

        public Builder showImageOnEmpty(int imageRes) {
            imageResForEmptyUri = imageRes;
            return this;
        }

        public Builder onImageScalType(int scalType) {
            this.scalType = scalType;
            return this;
        }

        public Builder showImageOnOnFail(int imageRes) {
            imageResOnFail = imageRes;
            return this;
        }

        public Builder showRoundCorner(int radius) {
            roundCornerRadius = radius;
            return this;
        }

        public Builder fadeInTime(int time) {
            fadeInTime = time;
            return this;
        }

        public ImageConfig build() {
            return new ImageConfig(this);
        }

    }
}
